import {
  disposeOrderCancel,
  disposeOrderSuperviseDetail,
  getDisposeOrderDetail,
  postSourceDelete
} from '@/apis/order.js'
import { Toast } from 'vant'

export const useOderDetail = () => {
  const superviseTotal = ref(0)
  const detailLoading = ref(false)
  const orderDetail = ref({
    id: '',
    orderNo: '',
    infoType: 0,
    infoLevel: 0,
    newOld: 0,
    sourceType: '',
    otherName: null,
    title: '',
    content: '',
    author: '',
    url: '',
    publishTime: 0,
    attachmentInfo: '',
    forwardCount: 0,
    commentCount: 0,
    thumbsCount: 0,
    viewCount: 0,
    plyCount: 0,
    memo: '',
    newsForwardRemark: null,
    status: '',
    followed: 0,
    del: 0,
    self: 0,
    createTime: 0,
    createName: '',
    updateTime: 0,
    updateName: '',
    sourceDeleted: null,
    sourceDeletedName: null,
    operation: {
      supervise: false,
      feedback: false,
      distribute: false,
      handle: false,
      back: false,
      cancel: false,
      terminate: false,
      addition: false,
      checkResponse: false,
      checkResponseHint: null
    },
    superviseNode: null,
    backNode: null,
    checkResponseNode: null,
    relatedCityList: [],
    relatedEnterpriseList: [],
    relatedUserList: [],
    attachment: [],
    relatedHandleList: [],
    timeoutStatus: 0,
    checkResponseTimeoutMinus: null,
    isSupervise: true
  })
  // 获取详情数据
  async function getOrderDetailData(query, fn = () => {}) {
    try {
      detailLoading.value = true
      const res = await getDisposeOrderDetail(query)
      const res2 = await disposeOrderSuperviseDetail(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          orderDetail.value = data
        } else {
          if(query.auditId && res?.msg === '待审核信息错误'){
            await getOrderDetailData({
                orderNo: query.orderNo
              },
              fn
            )
          } else {
            Toast(res?.msg)
            fn()
          }
        }
      }
      if (res2) {
        let { ret, data } = res2
        if (ret === 0 && data.length) {
          superviseTotal.value = data.length
        }
      }
      detailLoading.value = false
    } catch (error) {
      console.error(error)
      detailLoading.value = false
    }
  }

  // 撤销工单
  async function setDisposeOrderCancel(query) {
    try {
      detailLoading.value = true
      const res = await disposeOrderCancel(query)
      if (res) {
        let { ret, msg } = res
        if (ret === 0) {
          Toast(msg)
          await getOrderDetailData(query)
        }
      }
      detailLoading.value = false
    } catch (error) {
      console.error(error)
      detailLoading.value = false
    }
  }

  // 撤销工单
  async function setPostSourceDelete(query) {
    try {
      detailLoading.value = true
      const res = await postSourceDelete(query)
      if (res) {
        let { ret, msg } = res
        if (ret === 0) {
          Toast(msg)
          await getOrderDetailData(query)
        }
      }
      detailLoading.value = false
    } catch (error) {
      console.error(error)
      detailLoading.value = false
    }
  }

  return {
    detailLoading,
    orderDetail,
    superviseTotal,
    setDisposeOrderCancel,
    getOrderDetailData,
    setPostSourceDelete
  }
}
